package com.dream.keigezhushou.Activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dream.keigezhushou.Activity.Fragment.BuyFragment;
import com.dream.keigezhushou.Activity.Fragment.PlayFragment;
import com.dream.keigezhushou.Activity.Fragment.SingFragment;
import com.dream.keigezhushou.Activity.Fragment.SongFragment;
import com.dream.keigezhushou.R;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.tv_city)
    TextView mTvCity;
    @BindView(R.id.tv_seach)
    TextView mTvSeach;
    @BindView(R.id.iv_personal_center)
    ImageView mIvPersonalCenter;
    @BindView(R.id.tv_songs)
    TextView mTvSongs;
    @BindView(R.id.tv_sing)
    TextView mTvSing;
    @BindView(R.id.tv_play)
    TextView mTvPlay;
    @BindView(R.id.tv_KTVbuy)
    TextView mTvKTVbuy;
    @BindView(R.id.iv_choose)
    ImageView mIvChoose;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    private int bmpW;//横线图片宽度
    private ArrayList<Fragment> fragmentList;
    private int currIndex;//当前页卡编号
    private int offset;//图片移动的偏移量
    private GoogleApiClient mClient;
    private static final int CITY = 1;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        Window window = getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(getResources().getColor(R.color.primary));

        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
            ViewCompat.setFitsSystemWindows(mChildView, true);
        }


        InitImage();
        InitViewPager();

        updateTopIcon(0);


    }


    private void updateTopIcon(int arg0) {
        mTvSongs.setSelected(arg0 == 0 ? true : false);
        mTvSing.setSelected(arg0 == 1 ? true : false);
        mTvPlay.setSelected(arg0 == 2 ? true : false);
        mTvKTVbuy.setSelected(arg0 == 3 ? true : false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //定位页传数据

    }

    @OnClick({R.id.tv_city, R.id.tv_seach, R.id.iv_personal_center, R.id.tv_songs, R.id.tv_sing, R.id.tv_play, R.id.tv_KTVbuy, R.id.iv_choose})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_city:
                startActivityForResult(new Intent(this, CityActivity.class), CITY);
                break;
            case R.id.tv_seach:
                Toast.makeText(this, "进入搜索页面", Toast.LENGTH_SHORT).show();
                   startActivityForResult(new Intent(this, SeachActivity.class), CITY);
                break;
            case R.id.iv_personal_center:
                Toast.makeText(this, "进入个人中心", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_songs:
                mViewPager.setCurrentItem(0, false);
                updateTopIcon(0);
                break;
            case R.id.tv_sing:
                mViewPager.setCurrentItem(1, false);
                updateTopIcon(1);
                break;
            case R.id.tv_play:
                mViewPager.setCurrentItem(2, false);
                updateTopIcon(2);
                break;
            case R.id.tv_KTVbuy:

                mViewPager.setCurrentItem(3, false);
                updateTopIcon(3);
                break;

        }
    }


    public class txListener implements View.OnClickListener {
        private int index = 0;

        public txListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }
    }

    public void InitImage() {

        bmpW = BitmapFactory.decodeResource(getResources(), R.mipmap.yeqian).getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (screenW / 4 - bmpW) / 2;

        //imgageview设置平移，使下划线平移到初始位置（平移一个offset）
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        mIvChoose.setImageMatrix(matrix);
    }

    public void InitViewPager() {

        fragmentList = new ArrayList<Fragment>();
        Fragment btFragment = new SongFragment();
        Fragment secondFragment = new SingFragment();
        Fragment thirdFragment = new PlayFragment();
        Fragment fourthFragment = new BuyFragment();
        fragmentList.add(btFragment);
        fragmentList.add(secondFragment);
        fragmentList.add(thirdFragment);
        fragmentList.add(fourthFragment);

        //给ViewPager设置适配器
        mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        mViewPager.setCurrentItem(0);//设置当前显示标签页为第一页
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器
    }


    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        private int one = offset * 2 + bmpW;//两个相邻页面的偏移量

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            updateTopIcon(arg0);
           mViewPager.setCurrentItem(arg0);


            Animation animation = new TranslateAnimation(currIndex * one, arg0 * one, 0, 0);//平移动画
            currIndex = arg0;
            animation.setFillAfter(true);//动画终止时停留在最后一帧，不然会回到没有执行前的状态
            animation.setDuration(200);//动画持续时间0.2秒
            mIvChoose.startAnimation(animation);//是用ImageView来显示动画的
            int i = currIndex + 1;
            // Toast.makeText(MainActivity.this, "您选择了第"+i+"个页卡", Toast.LENGTH_SHORT).show();

        }
    }



}


class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragmentList;

    public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
        super(fm);
        this.fragmentList = list;

    }


    @Override
    public int getCount() {

        return fragmentList.size();
    }

    @Override
    public Fragment getItem(int arg0) {
        return  fragmentList.get(arg0);
    }





}
















